package com.block.merge.sdk.max;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.applovin.mediation.MaxAd;
import com.applovin.mediation.MaxAdRevenueListener;
import com.applovin.mediation.MaxError;
import com.applovin.mediation.MaxRewardedAdListener;
import com.applovin.mediation.ads.MaxRewardedAd;
import com.block.merge.sdk.Constant;
import com.block.merge.sdk.BlockAdSdk;
import com.block.merge.sdk.ad.BlocklVideoIAP;


public class BlocklMaxReward implements BlocklVideoIAP {
    private static BlocklMaxReward adManager;
    private MaxRewardedAd maxRewardedAd;
    private double mEcpm=0;
    private BlockAdSdk.OnAdShowListner mListner;

    public static BlocklMaxReward getInstance() {
        if (adManager == null) {
            adManager = new BlocklMaxReward();
        }
        return adManager;
    }

    private MaxRewardedAd getRewardedAd(String adUnitId) {
        return MaxRewardedAd.getInstance(adUnitId);
    }

    public void loadAd() {
        if (maxRewardedAd == null) {
            maxRewardedAd =getRewardedAd(Constant.REWARD_PLACEMENTID);
            maxRewardedAd.setListener(new MaxRewardedAdListener() {

                @Override
                public void onUserRewarded(MaxAd ad, com.applovin.mediation.MaxReward reward) {
//                Log.e("BloMerge","onUserRewarded");
                }

                @Override
                public void onAdLoaded(MaxAd ad) {
                    mEcpm=ad.getRevenue();
                    Log.d("BloMerge","onMaxRewardLoaded:"+mEcpm);
                }

                @Override
                public void onAdDisplayed(MaxAd ad) {
                    Log.e("BloMerge","onAdDisplayed"+ad.getNetworkName());
                }

                @Override
                public void onAdHidden(MaxAd ad) {
                    loadRewardAd();
                    if (mListner != null) {
                        mListner.reward(true);
                    }
                }

                @Override
                public void onAdClicked(MaxAd ad) {
                }

                @Override
                public void onAdLoadFailed(String adUnitId, MaxError error) {
                    mEcpm=0;
                    Log.d("BloMerge","onMaxRewardLoadFailed "+error.getCode()+ error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
                }

                @Override
                public void onAdDisplayFailed(MaxAd ad, MaxError error) {
                    Log.e("BloMerge","onAdDisplayFailed "+error.getCode() +error.getMessage()+error.getMediatedNetworkErrorCode()+error.getMediatedNetworkErrorMessage());
                    if (mListner != null) {
                        mListner.error(String.valueOf(error.getCode()));
                    }
                }
            });
            loadRewardAd();
        } else {
            if (!maxRewardedAd.isReady()) {
                loadRewardAd();
            }
        }
    }

    private void loadRewardAd(){
        mEcpm=0;
        maxRewardedAd.loadAd();
    }

    public void showRewardedVideo(Activity activity, BlockAdSdk.OnAdShowListner listner) {
        if (maxRewardedAd == null) {
            maxRewardedAd =getRewardedAd(Constant.REWARD_PLACEMENTID);
        }
        mListner=listner;
        if (maxRewardedAd.isReady()) {
            maxRewardedAd.showAd(activity);
        } else {
            loadRewardAd();
            if (listner!=null){
                listner.error("6000");
            }
        }
    }

    @Override
    public void showVideo(Activity activity, BlockAdSdk.OnAdShowListner listner) {
        showRewardedVideo(activity, listner);
    }

    @Override
    public double getEcpm() {
        return mEcpm;
    }
}
